package com.uyayo.ucenter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.ucenter.entity.MemberVisitorLog;
import com.uyayo.ucenter.mapper.MemberVisitorLogMapper;
import com.uyayo.ucenter.model.MemberVisitorLogCondition;
import com.uyayo.ucenter.service.MemberVisitorLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service("memberVisitorLogService")
public class MemberVisitorLogServiceImpl implements MemberVisitorLogService {

	private final MemberVisitorLogMapper memberVisitorLogMapper;

	public MemberVisitorLogServiceImpl(MemberVisitorLogMapper memberVisitorLogMapper) {
		this.memberVisitorLogMapper = memberVisitorLogMapper;
	}


	//@Cacheable(value = "getMemberById",key="#id")
	@Override
	public MemberVisitorLog getMemberVisitorLogById(Long id) throws Exception{
		return memberVisitorLogMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public List<MemberVisitorLog> selectMemberVisitorLog(MemberVisitorLogCondition memberVisitorLogCondition) {
		Example example = new Example(MemberVisitorLog.class);
		example.createCriteria();
		return memberVisitorLogMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<MemberVisitorLog> selectMemberVisitorLogByPaging(PageBean<MemberVisitorLog> pageBean, MemberVisitorLogCondition memberVisitorLogCondition) {

		List<MemberVisitorLog> userList;
		//分页处理
		List<OrderBy> orderByList = new ArrayList<>();
		orderByList.add(new OrderBy("createTime", "create_time"));
		orderByList.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderByList, pageBean.getOrderField(), pageBean.getOrderDirection());
		PageHelper.startPage(pageBean.getPageCurrent(), pageBean.getPageSize());
		
		Example example = new Example(MemberVisitorLog.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(memberVisitorLogCondition.getMemberId())){
			criteria.andEqualTo("memberId",memberVisitorLogCondition.getMemberId());
		}
		if(memberVisitorLogCondition.getBeginCreateTime()!=null || memberVisitorLogCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", memberVisitorLogCondition.getBeginCreateTime(), memberVisitorLogCondition.getEndCreateTime());
		}
		userList = memberVisitorLogMapper.selectByExample(example);

		//取记录总条数
		PageInfo<MemberVisitorLog> pageInfo = new PageInfo<>(userList);
		pageBean.setTotalCount(pageInfo.getTotal());
		pageBean.setList(userList);
		return pageBean;
	}
	

	@Override
	public int saveMemberVisitorLog(MemberVisitorLog memberVisitorLog) {
		return memberVisitorLogMapper.insertSelective(memberVisitorLog);
	}


	@Override
	public int updateMemberVisitorLog(MemberVisitorLog memberVisitorLog) {
		return memberVisitorLogMapper.updateByPrimaryKeySelective(memberVisitorLog);
	}

	@Override
	public int deleteMemberVisitorLog(Long id) {
		return memberVisitorLogMapper.deleteByPrimaryKey(id);
	}

}
